﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using slava.dao;

namespace DAO
{
    public class ProductRepository : IDao<Product>
    {
        skladEntities sklad;
        public ProductRepository()
        {
            this.sklad = new skladEntities();
        }

        public void Add(Product entity)
        {
            sklad.Products.Add(entity);
            sklad.SaveChanges();
        }

        public void Delete(int id)
        {
            Product prod = sklad.Products.FirstOrDefault(t => t.Id == id);
            sklad.Products.Remove(prod);
            sklad.SaveChanges();
        }

        public Product GetById(int id)
        {
            return sklad.Products.FirstOrDefault(t => t.Id == id);
        }

        public IEnumerable<Product> GetAll()
        {
            return sklad.Products;
        }

        public void Edit(Product entity)
        {
            var temp = sklad.Products.Single(x => x.Id == entity.Id);
            sklad.Entry(temp).CurrentValues.SetValues(entity);
            sklad.SaveChanges(); 
        }
    }
}
